package net.lcheng.shop.Controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import net.lcheng.model.OOrder;
import net.lcheng.service.OOrderService;
import net.lcheng.shop.vo.CurrentUserModel;
import net.lcheng.utils.Oorder2OrderModel;
import net.lcheng.vo.OrderModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zwr on 2016/7/19 0019.
 */
@Controller
public class OOrderController {
    @Autowired
    private OOrderService oOrderService;

    /**
     * 我的全部订单订单
     */
    @RequestMapping(value = "/order", method = RequestMethod.GET)
    public String orderGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();//获取当前用户
        if (currentUser.isAuthenticated()) {
            Session session = currentUser.getSession();
            CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            //获取订单列表
            List<OOrder> oOrderList = oOrderService.getOOrderByBuyerUserId(currentUserModel.getUserId(), null);
            List<OrderModel> orderModelList = Oorder2OrderModel.getOorder2OrderModel(oOrderList);

            model.addAttribute("orderModelList", orderModelList);
            model.addAttribute("oOrderList", oOrderList);
        } else {
            return "redirect:/login";
        }
        return "order";
    }

    /**
     * 代付款订单
     */
    @RequestMapping(value = "pre/payment", method = RequestMethod.GET)
    public String prePaymentGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();//获取当前用户
        Session session = currentUser.getSession();
        CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
        //获取代付款列表
        List<OOrder> oOrderList = oOrderService.getOOrderByBuyerUserId(currentUserModel.getUserId(), 0);
        List<OrderModel> orderModelList = Oorder2OrderModel.getOorder2OrderModel(oOrderList);
        model.addAttribute("orderModelList", orderModelList);
        model.addAttribute("oOrderList", oOrderList);
        return "pre_payment";
    }

    /**
     * 待收货
     */
    @RequestMapping(value = "order/inbound", method = RequestMethod.GET)
    public String orderInboundGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();//获取当前用户
        Session session = currentUser.getSession();
        CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
        //获取待收货列表
        List<OOrder> oOrderList = oOrderService.getOOrderByBuyerUserId(currentUserModel.getUserId(), 1);
        List<OrderModel> orderModelList = Oorder2OrderModel.getOorder2OrderModel(oOrderList);
        model.addAttribute("orderModelList", orderModelList);
        model.addAttribute("oOrderList", oOrderList);
        return "order_inbound";
    }

    /**
     * 已关闭
     */
    @RequestMapping(value = "order/closed", method = RequestMethod.GET)
    public String orderClosedGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();//获取当前用户
        Session session = currentUser.getSession();
        CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
        //获取已关闭列表
        List<OOrder> oOrderList = oOrderService.getOOrderByBuyerUserId(currentUserModel.getUserId(), -1);
        List<OrderModel> orderModelList = Oorder2OrderModel.getOorder2OrderModel(oOrderList);
        model.addAttribute("orderModelList", orderModelList);
        model.addAttribute("oOrderList", oOrderList);
        return "order_closed";
    }

    /**
     * 订单详情
     */
    @RequestMapping(value = "order/details", method = RequestMethod.GET)
    public String orderDetailsGet(Model model, @RequestParam String orderId) {
        if (StringUtils.isEmpty(orderId)){
            return "redirect:/error";
        }
        //根据orderId获取订单详情
        OOrder oOrder = oOrderService.getOOrderByOrderId(orderId);
        OrderModel orderModel=Oorder2OrderModel.getOorder2OrderModel(oOrder);
        model.addAttribute("orderModel",orderModel);
        model.addAttribute("oOrder",oOrder);
        return "order_details";
    }
}
